package com.ace3l.legal.backend.test;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.ace3l.legal.backend.dao.UserDao;
import com.ace3l.legal.backend.dao.LawFirmDao;
import com.ace3l.legal.backend.domain.User;
import com.ace3l.legal.backend.domain.Role;
import com.ace3l.legal.backend.domain.LawFirm;

/**
 * Created by IntelliJ IDEA.
 * User: ageer
 * Date: Jul 22, 2008
 * Time: 7:16:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class CreateUserServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        WebApplicationContext appCtx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        UserDao userDao = (UserDao)appCtx.getBean("userDao");
        LawFirmDao lawFirmDao = (LawFirmDao)appCtx.getBean("lawFirmDao");

        String username = req.getParameter("un");
        String password = req.getParameter("pw");
        
        LawFirm mlf = new LawFirm();
        mlf.setContactFirstName("Allen");
        mlf.setContactLastName("Geer");
        mlf.setContactPhone("888-555-1212");
        mlf.setEmail("allengeer@gmail.com");
        mlf.setEmailVerified(false);
        mlf.setFirmAddress1("add1");
        mlf.setFirmAddress2("add2");
        mlf.setFirmCity("Houston");
        mlf.setFirmState("TX");
        mlf.setFirmZip("77025");
        mlf.setLawFirmName("A Law Firm");
        mlf.setLawFirmStatus("NEW");


        Role myRole = new Role();
        myRole.setRole("LAWFIRM");
        User myUser = new User();
        myUser.setUsername(username);
        myUser.setPassword(password);
        myUser.addRole(myRole);
        myUser = userDao.saveUser(myUser);

        mlf.setUser(myUser);
        try {
            lawFirmDao.saveLawFirm(mlf);
            Writer out = res.getWriter();
            out.write("Success");
        } catch (Exception e) {
            e.printStackTrace();
            Writer out = res.getWriter();
            out.write("Error: " + e.getMessage());
        }


//        boolean auth = userDao.authenticateUser(username,password);

    }
}
